import axios from "axios";
import { Message } from "element-ui";
import store from "@/store/modules/user";

// create an axios instance
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  // timeout: 5000, // request timeout
});
// 添加请求拦截器
service.interceptors.request.use(
  function (config) {
    // 在发送请求之前做些什么
    // config.headers["Content-Type"] = "application/x-www-form-urlencoded"
    config.headers["Content-Type"] = "application/json;charset=utf-8";
    config.headers["authorization"] = store.state.token;
    return config;
  },
  function (error) {
    return Promise.reject(error);
  }
);

// 添加响应拦截器
service.interceptors.response.use(
  function (response) {
    return response;
  },
  function (error) {
    console.log(error.response);
    const { msg } = error.response.data.meta;
    Message.error(msg)
  }
);

export default service;
